package com.wzs.setting.service.impl;

import com.wzs.exception.LoginException;
import com.wzs.setting.dao.ReaderMapper;
import com.wzs.setting.domain.Like;
import com.wzs.setting.domain.Readr;
import com.wzs.setting.service.ReaderService;
import com.wzs.utils.MD5Util;
import com.wzs.utils.UUIDUtil;
import com.wzs.vo.PaginationVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReaderServiceImpl implements ReaderService {

    @Resource
    private ReaderMapper readerMapper;

    @Override
    public Readr login(String username, String password) throws LoginException {

        Map<String, String> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        Readr readr = readerMapper.login(map);
        if (readr == null) {
            throw new LoginException("账号或密码错误");
        }
        return readr;
    }

    @Override
    public boolean register(Readr readr) {

        boolean flag = true;
        String id = UUIDUtil.getUUID();
        String password = MD5Util.getMD5(readr.getPassword());
        readr.setId(id);
        readr.setPassword(password);

        int count = readerMapper.register(readr);

        if (count != 1) {
            flag = false;
        }

        return flag;
    }

    @Override
    public boolean updateReader(Readr readr) {

        boolean flag = true;

        int count = readerMapper.updateByPrimaryKeySelective(readr);

        if (count !=1){
            flag= false;
        }

        return flag;
    }

    @Override
    public boolean like(String novelId, String readerId) {

        boolean flag = true;

        Map<String, String> map = new HashMap<>();
        String id = UUIDUtil.getUUID();
        map.put("novelId", novelId);
        map.put("readerId", readerId);
        map.put("id", id);

        int count = readerMapper.like(map);
        if (count !=1){
            flag= false;
        }

        return flag;
    }

    @Override
    public PaginationVO<Like> pageList(HashMap<String, Object> map) {

        //取得total
        int total = readerMapper.getTotalByCondition(map);
        //取得list
        List<Like> dataList = readerMapper.getLikeListByCondition(map);
        //创建一个vo对象,将total和datalist封装到vo中
        PaginationVO<Like> vo = new PaginationVO<>();
        vo.setTotal(total);
        vo.setDataList(dataList);

        //将vo返回
        return vo;
    }

}
